package com.yang.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yang.dao.BaseDAO;
import com.yang.dao.ResourceDao;
import com.yang.entity.Permissions;
import com.yang.entity.Resource;
import com.yang.entity.Roles;
import com.yang.model.MenuBean;
import com.yang.model.Page;
import com.yang.service.ResourceService;

@Service
@Transactional
public class ResourceServiceImpl implements ResourceService {

	@Autowired
	private ResourceDao resourceDao;
	@Autowired
	private BaseDAO<Resource> baseDao;
	
	public Resource createResource(Resource resource) {
		resourceDao.createResource(resource);
		return resource;
	}

	@Transactional
	public Resource updateResource(Resource resource) {
		resourceDao.updateResource(resource);
		return resource;
	}

	public boolean deleteResource(String resourceId) {
		//删除菜单需要查看该菜单是否有子菜单，有子菜单就不能删除
		String hql = "from Resource where parentId=:parentId";
		Map<String,Object> param = new HashMap<String, Object>();
		param.put("parentId", resourceId);
		List<Resource> result =  baseDao.find(hql, param);
		if(result!=null && !result.isEmpty()){	//具有子菜单，不删除，直接返回
			return false;
		}else{
			resourceDao.deleteResource(resourceId);
			return true;
		}
	}

	public Resource findOne(String resourceId) {
		return resourceDao.findOne(resourceId);
	}

	public List<Resource> findAll() {
		return resourceDao.findAll();
	}

	public Page<Resource> getPage(String nowPage, String pageSize) {
		String hql = "from Resource";
		Integer pageNo = pasrePageNow(nowPage);
		Integer pageSizeInt = Integer.parseInt(pageSize);
		List<Resource> resources = baseDao.find(hql, new Object[] {}, pageNo, pageSizeInt);
		long totalCount = baseDao.count(hql);
		Page<Resource> page = new Page<Resource>(pageSizeInt);
		page.setTotalCount(totalCount);
		page.setResult(resources);
		page.setPageNo(pageNo);
		return page;
	}

	/**
	 * 转化前端传来的当前页
	 * @param nowPage	当前页
	 * @return
	 */
	private Integer pasrePageNow(String nowPage){
		if(null!=nowPage){
			Integer pageNo = Integer.parseInt(nowPage);
			return pageNo;
		}
		return 1;
	}

	public MenuBean getAllMenus() {
		String hql ="from Resource where type=0";
		List<Resource> resList = baseDao.find(hql);
		
		MenuBean menus = new MenuBean();
		for (Resource resource : resList) {
			if(resource.getParentId().equals("0")){	//第一级菜单
				menus.getMenus().add(resource);
				continue;
			}else{
				menus.getSecondMenu().add(resource);
			}
		}
		return menus;
	}

	public MenuBean getAllMenus(Set<Roles> roles) {
		List<String> permissionsStr = new ArrayList<String>();
		for (Roles role : roles) {
			for (Permissions per : role.getPermissions()) {
				permissionsStr.add(per.getpCode());
			}
		}
		
		if(permissionsStr.contains("ALL")){	//具有所有权限
			return getAllMenus();
		}
		String hql ="from Resource where type=0 and permission in :list";
		List<Resource> resList = baseDao.findIn(hql, permissionsStr);
		MenuBean menus = new MenuBean();
		for (Resource resource : resList) {
			if(resource.getParentId().equals("0")){	//第一级菜单
				menus.getMenus().add(resource);
				continue;
			}else{
				menus.getSecondMenu().add(resource);
			}
		}
		return menus;
	}

	@Override
	public Resource save(Resource res) {
		baseDao.save(res);
		//保存之后还要修改全路径
		Resource parent = baseDao.get(Resource.class, res.getParentId());
		String p_ids = parent.getParentIds();
		res.setParentIds(p_ids+"|"+res.getId());
		baseDao.update(res);
		return res;
	}
}
